package cn.wolfcode.java39.rocketmqproducerdemo2.api._04_push_pull;

import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.PullResult;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;

import java.util.List;

public class ConsumerPull {
    public static void main(String[] args) throws Exception {
        //创建一个拉取消息的消费者对象
        DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("wolfcode-consumer");
        //设置名字服务地址
        consumer.setNamesrvAddr("192.168.136.128:9876;192.168.136.129:9876");
        //设置消费模式
        consumer.setMessageModel(MessageModel.CLUSTERING);//默认集群

        //启动消费者
        consumer.start();
        PullResult pullResult = consumer.pull(new MessageQueue("04-push-pull", "broker-a", 2), "*", 0, 10);
        List<MessageExt> messageExts = pullResult.getMsgFoundList();
        for (MessageExt messageExt : messageExts) {
            System.out.println("接受到的消息:"+ new String(messageExt.getBody()));
        }
    }
}
